using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for LibraryControl.
	/// </summary>
	public class LibraryControl : AdminUserControl
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public LibraryControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Delete", "Delete this library", new ActionExecuteDelegate(DeleteLibrary));
      actionList.Add("Save latest...", "Save the latest version of this library", 
        new ActionExecuteDelegate(SaveLatest));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LibraryControl));
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.label1 = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.TextBox();
      this.lastPublishedLabel = new System.Windows.Forms.TextBox();
      this.label2 = new System.Windows.Forms.Label();
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.proceduresLabel = new System.Windows.Forms.Label();
      this.proceduresListBox = new System.Windows.Forms.ListBox();
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
      this.SuspendLayout();
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(666, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 408);
      this.uiActionsControl.TabIndex = 0;
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(40, 16);
      this.label1.TabIndex = 1;
      this.label1.Text = "Name:";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.nameLabel.Location = new System.Drawing.Point(88, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.ReadOnly = true;
      this.nameLabel.Size = new System.Drawing.Size(528, 13);
      this.nameLabel.TabIndex = 2;
      this.nameLabel.Text = "nameLabel";
      // 
      // lastPublishedLabel
      // 
      this.lastPublishedLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.lastPublishedLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.lastPublishedLabel.Location = new System.Drawing.Point(88, 24);
      this.lastPublishedLabel.Name = "lastPublishedLabel";
      this.lastPublishedLabel.ReadOnly = true;
      this.lastPublishedLabel.Size = new System.Drawing.Size(528, 13);
      this.lastPublishedLabel.TabIndex = 6;
      this.lastPublishedLabel.Text = "lastPublishedLabel";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 24);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(80, 16);
      this.label2.TabIndex = 5;
      this.label2.Text = "Last published:";
      // 
      // pictureBox1
      // 
      this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(624, 8);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 32);
      this.pictureBox1.TabIndex = 7;
      this.pictureBox1.TabStop = false;
      // 
      // proceduresLabel
      // 
      this.proceduresLabel.AutoSize = true;
      this.proceduresLabel.Location = new System.Drawing.Point(8, 45);
      this.proceduresLabel.Name = "proceduresLabel";
      this.proceduresLabel.Size = new System.Drawing.Size(99, 13);
      this.proceduresLabel.TabIndex = 8;
      this.proceduresLabel.Text = "Used in procedures";
      // 
      // proceduresListBox
      // 
      this.proceduresListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                  | System.Windows.Forms.AnchorStyles.Left)));
      this.proceduresListBox.FormattingEnabled = true;
      this.proceduresListBox.Location = new System.Drawing.Point(11, 61);
      this.proceduresListBox.Name = "proceduresListBox";
      this.proceduresListBox.Size = new System.Drawing.Size(225, 329);
      this.proceduresListBox.TabIndex = 9;
      // 
      // LibraryControl
      // 
      this.Controls.Add(this.proceduresListBox);
      this.Controls.Add(this.proceduresLabel);
      this.Controls.Add(this.pictureBox1);
      this.Controls.Add(this.lastPublishedLabel);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.nameLabel);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "LibraryControl";
      this.Size = new System.Drawing.Size(816, 408);
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
      this.ResumeLayout(false);
      this.PerformLayout();

    }
		#endregion

    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox nameLabel;
    private System.Windows.Forms.TextBox lastPublishedLabel;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.PictureBox pictureBox1;
    private Label proceduresLabel;
    private ListBox proceduresListBox;


    private Library library;
    public void LoadLibrary(Library library)
    {
      this.library = library;
      nameLabel.Text = library.Name;
      lastPublishedLabel.Text = library.LastPublished.ToString();
      proceduresLabel.Visible = (library.Server.SchemaVersion >= SchemaVersion.Version70);
      proceduresListBox.Visible = (library.Server.SchemaVersion >= SchemaVersion.Version70);
      if (library.Server.SchemaVersion >= SchemaVersion.Version70)
      {
        proceduresListBox.Items.Clear();
        for (int i = 0; i < library.ProcedureCount; i++)
        {
          string procedure = library.Procedure(i);
          if (procedure != null)
            proceduresListBox.Items.Add(procedure);
        }
      }
    }

    private string DeleteLibrary()
    {
      if (MessageBox.Show("Delete this library?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          library.Delete();
        }
        OnItemDeleted();
        Dispose();
      }
      return null;
    }

    private string SaveLatest()
    {
      VersionControl.SaveVersion(library.Versions.LatestVersion);
      return null;
    }
	}
}
